<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.nick_name"
        clearable
        placeholder="用户名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-input
        v-model="listQuery.phone_number"
        clearable
        placeholder="电话号码"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-select
        v-model="listQuery.gender"
        clearable
        placeholder="性别"
        style="width: 130px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option :value="1" label="男" />
        <el-option :value="2" label="女" />
        <el-option :value="0" label="未知" />
      </el-select>
      <el-select
        v-model="listQuery.from"
        clearable
        placeholder="登陆方式"
        style="width: 130px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option :value="1" label="小程序" />
      </el-select>
      <el-button
        v-waves
        style="margin-left: 10px"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-table
      ref="itemDataTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top: 20px"
      @sort-change="tableSortChange"
    >
      <el-table-column
        label="ID"
        prop="id"
        align="center"
        width="80"
      />
      <el-table-column label="头像" width="80" align="center">
        <template slot-scope="{ row: { avatar_url }}">
          <img
            v-if="avatar_url === ''"
            style="width: 50px"
            src="@/assets/static/image/noImg.png"
          >
          <a v-else :href="avatar_url" target="_blank">
            <img
              :src="avatar_url"
              style="width: 50px"
            >
          </a>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="{ row: { nick_nameWrapper }}">
          <span v-html="nick_nameWrapper" />
        </template>
      </el-table-column>
      <el-table-column
        label="openid"
        prop="openid"
        align="center"
      />
      <el-table-column label="电话号码" align="center" width="160">
        <template slot-scope="{ row: { phone_numberWrapper }}">
          <span v-html="phone_numberWrapper" />
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" width="100" prop="from" sortable="custom">
        <template slot-scope="{ row: { gender }}">
          <el-tag v-if="gender === 1" type="success">男</el-tag>
          <el-tag v-else-if="gender === 2" type="warning">女</el-tag>
          <el-tag v-else type="danger">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" width="100" prop="from" sortable="custom">
        <template slot-scope="{ row: { from }}">
          <el-tag v-if="from === 1">小程序</el-tag>
          <el-tag v-else type="danger">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center" prop="status" sortable="custom">
        <template slot-scope="{ row }">
          <it-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            active-text="开启"
            inactive-text="停用"
            @change="handleWeChatUserStatus($event, row.id, 'status')"
          />
        </template>
      </el-table-column>
      <el-table-column label="第三方授权" width="150" align="center">
        <template slot-scope="{ row }">
          <it-switch
            v-model="row.third_status"
            :active-value="1"
            :inactive-value="0"
            active-text="开启"
            inactive-text="停用"
            @change="handleWeChatUserStatus($event, row.id, 'third_status')"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="加入日期"
        prop="create_time"
        sortable="custom"
        align="center"
        width="200"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="handleFilter"
    />
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import TnInnerTextSwitch from '@/components/TNInnerTextSwitch'
import { wrapperKeyword } from '@/utils'
import { getWeChatUserList, updateWeChatUser } from '@/api/wechat-user'

export default {
  name: 'WeChatUserList',
  components: { Pagination, 'it-switch': TnInnerTextSwitch },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      listLoading: false,
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        nick_name: '',
        phone_number: '',
        gender: '',
        from: ''
      },
      list: []
    }
  },
  watch: {
    list(val) {
      val.forEach(user => {
        user.nick_nameWrapper = wrapperKeyword(this.listQuery['nick_name'] || '', user.nick_name)
        user.phone_numberWrapper = wrapperKeyword(this.listQuery['phone_number'] || '', user.phone_number)
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.getList()
    },
    handleWeChatUserStatus(value, id, field) {
      updateWeChatUser({
        id,
        field,
        value
      }).then(response => {
        this.$message({
          message: response.msg,
          type: 'success',
          duration: 2000
        })
      })
    },
    tableSortChange(data) {
      const { prop, order } = data
      // console.log(prop, order, data)
      this.listQuery.sort_prop = prop
      this.listQuery.sort_order = order
      this.getList()
    },
    getList() {
      this.listLoading = true
      getWeChatUserList(this.listQuery).then(response => {
        const { data, total } = response
        this.list = data
        this.total = total
        this.listLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
